﻿using System;
using System.Web;
using MyHealthByDesign.DataAccess.AccountDA;
using MyHealthByDesign.Helper;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace MyHealthByDesign.Domain
{
    public class Domain
    {
        AccountRepository accountRepository = new AccountRepository();
        
        public bool IsValidUser(string email, string password)
        {
            bool validUser = false;
            var user = accountRepository.GetUser(email, password);

            if (user != null)
            {
                validUser = true;
                HttpContext.Current.Session["User"] = user;
            }
            return validUser;
        }


        public bool SendEmail(string email)
        {
            bool isSuccess = false;
            string password = accountRepository.GetPassword(email);

            try
            {
                if (!string.IsNullOrWhiteSpace(password))
                {
                    var mail = new Mail();
                    mail.SendEmailAddress(email, password);
                    isSuccess = true;
                }
                else
                {
                    //no action needed since IsSuccess is false by default
                }
            }
            catch (Exception ex)
            {
                //no action needed since IsSuccess is false by default
            }

            return isSuccess;
        }

        public bool IsEmailRegistered(string email)
        {
            bool isRegistered = true;
            string password = accountRepository.GetPassword(email);

            if(string.IsNullOrWhiteSpace(password))
            {
                isRegistered = false;
            }

            return isRegistered;
        }

        public bool RegisterUser(string lastname, string firstname, string birthDate, string city, string zipCode, string email, string password, string mobile, string textFlag, string sex, string ethnicity)
        {
            bool success = false;
           
            int insertRowCount = accountRepository.RegisterUser(lastname, firstname, birthDate, city, zipCode, email, password, mobile, textFlag, sex, ethnicity);
                
            if(insertRowCount > 0)
            {
                success = true;
                var user = new User
                               {
                                   Email = email,
                                   Firstname = firstname,
                                   Lastname = lastname,
                                   BirthDate = birthDate,
                                   City = city,
                                   ZipCode = zipCode,
                                   Password = password,
                                   MobilePhone = mobile,
                                   Gender = sex,
                                   Ethnicity = ethnicity,
                                   TextFlag = textFlag
                               };

                HttpContext.Current.Session["User"] = user;
            }

            return success;
        }

        public bool IsUserLoggedIn()
        {
            bool isLoggedIn = false;
            
            if (HttpContext.Current.Session["User"] != null)
            {
                isLoggedIn = true;
            }

            return isLoggedIn;
        }

        // retrieve the questions for a provided category
        public List<Question> RetrieveQuestions(int categoryID)
        {
            return accountRepository.GetQuestions(categoryID);
        }

        // obtain question answer values and save to database
        public bool SaveQuestionAnswers(ContentPlaceHolder cph)
        {
            try
            {
                if (HttpContext.Current.Session["User"] != null)
                {
                    Dictionary<Question, NameValueCollection> returnValue =
                        new Dictionary<Question, NameValueCollection>();
                    List<QuestionAnswer> lstQa = new List<QuestionAnswer>();
                    
                    User user = (User)HttpContext.Current.Session["User"];
                    int nCount = 0;


                    List<Question> lstQs = (List<Question>) HttpContext.Current.Session["Questions"];

                    foreach (Question q in lstQs)
                    {
                        returnValue = q.RetrieveQuestionValues(cph);
                        foreach (var item in returnValue)
                        {
                            NameValueCollection nvc = item.Value;

                            for (nCount = 0; nCount < nvc.Count; nCount++)
                            {
                                QuestionAnswer qa = new QuestionAnswer();
                                qa.QuestionId = q.Id;
                                qa.UserId = user.UserId;
                                qa.FieldName = nvc.GetKey(nCount);
                                qa.FieldValue = nvc.Get(nCount);

                                lstQa.Add(qa);
                            }

                        }
                    }

                   // insert to database
                   return accountRepository.AnswersRecorded(lstQa);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}